#!/bin/sh
# Monlog Pusher init script.
. /etc/utils.sh

APP="monlog_pusher"
BINARY="/app/spacecast/monlog_pusher"
FLAGS="-logtostderr"

running() {
  [ -n "$(pgrep -f "$BINARY")" ]
}

# Bootstrap
mkdir -p "/tmp/monlog"
mount_once "/tmp/monlog" "/etc/monlog"

case "$1" in
  start)
    if running; then
      echo "Monlog pusher is already running!"
    else
      (
        # Bootstrap
        wait-until-created /etc/monlog/oauth2_config.json
        babysit_start 10 "$APP" "$BINARY" "$FLAGS"
      ) &
    fi
    ;;
  stop)
    pkill -f "$BINARY"
    ;;
  restart|reload)
    $0 stop;$0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
